/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : Jul 15, 2012  7:26:10 PM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : apps.business.process.construction.payment.request
 * File Name : RequestPaymentHighRiseProcessing.java
 * TODO
 */
package apps.business.process.construction.payment.request;

import java.util.List;
import org.apache.log4j.Logger;
import apps.business.process.SyncProcessorCommons;
import apps.promis.out.construction.VwSyncRequestPaymentHighRiseAction;
import promis.utility.CommonService;
import sap.business.mm.outbound.VwSyncRequestPaymentService;
import jpa.entitys.promis.construction.VwSyncRequestPaymentHRise;
import jpa.entitys.synchro.staging.MmLivcontractorOutb;

/**
 * @author chatree
 *
 */
public class RequestPaymentHighRiseProcessing extends SyncProcessorCommons {
 
	private static final Logger log = Logger.getLogger(RequestPaymentHighRiseProcessing.class);
	/**
	 * 		String company = "SP";
			String project = "SPLVILLE-PRAGSA";
			String documentNo = "PB-1206-000002";
			String status = "A";
			String syncIsstaging = "Y";
			String refType = "CW";
	 * @param company
	 * @param project
	 * @param documentNo
	 * @param refType
	 * @param status
	 * @param syncIsstaging
	 * @throws Exception
	 */
	public void Processing(String company, String project, String documentNo, String refType, String status, String syncIsstaging) throws Exception {		
		try{
			UPD_PROMISUseTime(true, false, null);			
			List<MmLivcontractorOutb> value_list = new VwSyncRequestPaymentHighRiseAction().CreateToStaging(company, project, documentNo, refType, status, syncIsstaging);
			UPD_PROMISUseTime(false, true, null);			
			System.out.println(" Create ["+ company +" || " + project +" || "+ documentNo +" || "+ refType +" || " + status +"] to Staging ---> Size=" + CommonService.getSize(value_list));
			
			if(!CommonService.IsNull(value_list)){
				UPD_SENDUseTime(true, false, value_list.get(0).getInstanceNo());
				new VwSyncRequestPaymentService().SendToSAP(value_list, null);
				UPD_SENDUseTime(false, true, value_list.get(0).getInstanceNo());
				System.out.println("Sending ["+ company +" || " + project +" || "+ documentNo +" || "+ refType +" || " + status +"] to SAP--->  size=" + CommonService.getSize(value_list));
			}else
				System.out.println("Find not Found ["+ company +" || " + project +" || "+ documentNo +" || "+ refType +" || " + status +"] !!! -->.");
			
		}catch(Exception e){
			log.error(e.getMessage());
			System.err.println(e.getMessage());
			throw e;
		}
	}
	
	
	@Deprecated
	private void Start() throws Exception {
		
		try{

			String company = "SP";
			String project = "SPK-R";
			String documentNo = "PW-1207-000300";
			String status = "A";
			String syncIsstaging = "N";
			String refType = "CW";  //CT
			
			List<MmLivcontractorOutb> value_list = new VwSyncRequestPaymentHighRiseAction().CreateToStaging(company, project, documentNo, refType, status, syncIsstaging);
			if(!CommonService.IsNull(value_list))
				System.out.println("Create to Staging  value_list : " + value_list.size());
			else
				System.out.println("Create to Staging  value_list : 0");
			
			new VwSyncRequestPaymentService().SendToSAP(value_list, null);
				System.out.println("Send to SAP.");
			
		}catch(Exception e){
			log.error(e.getMessage());
			System.err.println(e.getMessage());
			throw e;
		}
	}
	
	private void ReRunAll() throws Exception {
		try{
			
			String company ="SP";
			String project = "VSP";

			List<VwSyncRequestPaymentHRise> lst = new VwSyncRequestPaymentHighRiseAction().ReRunAll(company, project);
			if(!CommonService.IsNull(lst)){
				
				for(VwSyncRequestPaymentHRise value : lst)
					
					Processing(value.getCompany(), value.getProject(), value.getRequestNo(), value.getRefType(), value.getStatus(), value.getSyncIsstaging());
						
			}
		}catch(Exception e){
			log.error(e.getMessage());
			System.err.println(e.getMessage());
			throw e;
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try{
			
			new RequestPaymentHighRiseProcessing().Start();
			
		}catch(Exception e){
			log.error(e.getMessage());
			System.err.println(e.getMessage());
			log.error(e);
		}
	}

}
